﻿<%@ Page Language="C#" MasterPageFile="~/MasterTheme/Auto.master" AutoEventWireup="true"
    Inherits="xIx.Web.Portal.PageBase" Title="Untitled Page" %>

<%@ Register Src="~/UserControls/LoginPan.ascx" TagName="LoginPan" TagPrefix="uc3" %>
<%@ Register Src="~/UserControls/DownLoad.ascx" TagName="download" TagPrefix="form" %>
<%@ Register Assembly="xIx.Web.Portal" Namespace="xIx.Web.Portal" TagPrefix="DS" %>
<script runat="server" language="c#">
    private string mdbFile;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (string.IsNullOrWhiteSpace(Request["Y"]) || string.IsNullOrWhiteSpace(Request["P"]))
        {
            downloadLink.Visible = false;
            Response.Write("ERROR.");
            return;
        }
        var dbName = System.Configuration.ConfigurationManager.AppSettings["dedorm"];
        if (string.IsNullOrEmpty(dbName))
        {
            downloadLink.Visible = false;     
            return;
        }
        var dbConn = System.Configuration.ConfigurationManager.ConnectionStrings[dbName];
        if (dbConn == null)
        {
            downloadLink.Visible = false;
            return;
        }
        downloadLink.NavigateUrl = string.Format("DownLoad.aspx?Y={0}&P={1}", Request["Y"], Request["P"]);
        string dbpath= string.Empty;
        if(dbConn.ConnectionString.Contains("[AppRoot]"))
        {
            int start = dbConn.ConnectionString.IndexOf("[AppRoot]") + "[AppRoot]".Length;
            dbpath = "~" + dbConn.ConnectionString.Substring(start).Replace("\\", "/");
        }
        mdbFile = dbpath;
        
        //downloadLink.NavigateUrl = dbpath;
        using (xIx.Data.DbHelper dh = new xIx.Data.DbHelper("dedorm", null))
        {            
            if (Request["Y"] == "全部")
            {
                GridView1.DataSource = dh.Table[Request["P"]].Select();
            }
            else
            {
                int year = 0;
                year = int.Parse(Request["Y"]);
                GridView1.DataSource = dh.Table[Request["P"]].Where("date>=@Start and date<@End").Select((year * 10000), ((year + 1) * 10000));
            }
            GridView1.DataBind();
        }
        
    }
    public static void MsgBox1(string _Msg)
    {
        string StrScript;
        StrScript = ("<script language=javascript>");
        StrScript += "var retValue=window.confirm('" + _Msg + "');";
        StrScript += ("</"+"script>");
        System.Web.HttpContext.Current.Response.Write(StrScript);

    }


    protected void downloadLink_Click(object sender, EventArgs e)
    {
        string name = mdbFile;// Request["url"];
        int k = name.LastIndexOf("/");
        string filename = name.Remove(0, k + 1);

        string filePath = Server.MapPath(name);//路径

        //以字符流的形式下载文件 
        System.IO.FileStream fs = new System.IO.FileStream(filePath, System.IO.FileMode.Open);
        byte[] bytes = new byte[(int)fs.Length];
        fs.Read(bytes, 0, bytes.Length);
        fs.Close();
        Response.ContentType = "application/octet-stream";
        //通知浏览器下载文件而不是打开 
        Response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(filename, System.Text.Encoding.UTF8));
        Response.BinaryWrite(bytes);
        Response.Flush();
        Response.End();

    }    
</script>


<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" runat="Server">
<script language="javascript" type="text/javascript">
//    ShowWindow = function (caption, fieldSetId, func) {
//        if (func != null) {
//            func();
//        }
//        var documentWidth = document.documentElement.scrollWidth;
//        var documentHeight = document.documentElement.scrollHeight;
        //var form = document.getElementById("downloadForm");
        //$('#downloadForm').show("downloadForm");
        //        $('.overlay').width(documentWidth).height(documentHeight).css({ left: 0, top: 0 });
        //        $('.overlay').fadeIn();
        //$('#windowCaption').html(caption);
        //$('.formPanel').hide();
        //$('#' + fieldSetId).show();
        //$('.content').fadeIn();
    //};
</script>
    <div style=" text-align:right">
        <%--<asp:LinkButton ID="downloadLink" runat="server" onclick="downloadLink_Click">【下载附件】</asp:LinkButton>--%>
        <asp:HyperLink runat="server" ID="downloadLink" Target="_blank">【下载附件】</asp:HyperLink>
        <%--<asp:LinkButton ID="downloadLink" runat="server" OnClick="ShowDownload" OnClientClick="return ShowWindow('下载附件', 'download');">【下载附件】</asp:LinkButton>--%>
    </div>
    <asp:GridView ID="GridView1" runat="server" ViewStateMode="Disabled">
    </asp:GridView>
    <%--<form:download ID="downloadForm" runat="server" />--%>
</asp:Content>
